<?php
    if(!$this->session->userdata('logged_in')) {
        redirect('logout');
    } else if($this->session->userdata('role') === 'viewer') {
        redirect('forbidden');
    }
?>
<div id="group_page">
    <div id="group_wrapper">
        <div id="group_topbar">
            <?php
                if($role_id == 2 || $rolename == 'author') {
                    echo "<p id='all_groups_header'>Groups I Manage</p>";
                }
                else if($role_id == 1 || $rolename == 'admin') {
                    echo "<p id='all_groups_header'>All Groups</p>";
                }
            ?>
       
            <form action="search-group" method="post" id="form_search">
                <input type="text" name="txt_find" id="txt_find" />
                <input type="submit" name="btn_find_group" id="btn_find_group" value="Find Group" />
            </form>
             <?php
            if($role_id == 1 || $rolename == 'admin') {
                echo anchor('groups/create', 'Create Group', 'id="create_group"')."<br />";         
            }
         ?>
                        
        </div>
        
         <div id="centerer">
        <?php
        if($groups == null || count($groups) == 0) {
            echo "<p id='p_no_group'>There are  no groups to display.</p>";
        }
        else {
                foreach ($groups as $group_entry) {
                    $group_id = $group_entry['group_id'];
                    $group_name = $group_entry['group_name'];
                    $group_desc = $group_entry['group_desc'];
                    $date_created = $group_entry['date_created'];
                    
                    if(strlen($group_name) > 22) {
                        $str_arr = str_split($group_name);
                        $str_arr[22] = "...";
                        
                        for($i = 23; $i < count($str_arr); $i++) {
                            $str_arr[$i] = "";
                        }
                        
                        $group_name = implode($str_arr);
                    }
                    
                    if(strlen($group_desc) > 70) {
                        $str_arr2 = str_split($group_desc);
                        $str_arr2[70] = "...";
                        
                        for($i = 71; $i < count($str_arr2); $i++) {
                            $str_arr2[$i] = "";
                        }
                        
                        $group_desc = implode($str_arr2);
                    }
        ?>
       
        <div class="group_holder">
            <div class="group_name_holder"><?php echo anchor('groups_c/edit_group/'.$group_id, $group_name); ?></div>
            <div class="group_desc_holder"><?php echo $group_desc; ?></div>
            
            <div id="aligner">
                <div class="group_edit"><?php echo anchor('groups/edit/'.$group_id, 'Edit');?></div>
                <div class="group_manage"><?php echo anchor('groups/manage/'.$group_id, 'Subscribers');?></div>
                <?php
                  if($role_id == 1 || $rolename == 'admin') {
                     echo "<div class='group_delete'>".anchor('groups_c/sure_delete/'.$group_id, 'Delete', array('onClick' => "return confirm('Are you sure you want to delete this Group?')"))."</div>";
                  }
                ?>
            </div>
        </div>
        
        <?php }} ?> </div>
        
    </div>
</div>